package com.taxigang.client.uibinder;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.client.uibinder.TaxiGangRoute.CreateMode;
import com.taxigang.client.uibinder.base.CreateStep;
import com.taxigang.shared.TaxiCompanyInfo;
import com.taxigang.shared.TaxiGangInfo;

public class CreateStep2a extends CreateStep {


	private static CreateStep2aContentUiBinder uiBinder = GWT
			.create(CreateStep2aContentUiBinder.class);

	interface CreateStep2aContentUiBinder extends
			UiBinder<Widget, CreateStep2a> {
	}

	@UiField
	Button backButton;

	@UiField
	Label enterTaxiNumber;

	@UiField
	Button okButton;
	
	@UiField
	Button bTest;
	
	@UiField
	Panel panelTaxiCompanies;

//	@UiField
//	TaxiCompanyCellTable taxiCompanyCellTable;

	@UiField TextBox areaCodeTextBox;
	@UiField TextBox telNoTextBox;
	
	@UiField
	Label headerLabel;

	FlexTable taxiCompanyTable;
	public CreateStep2a() {
		initWidget(uiBinder.createAndBindUi(this));
		bindUI();
		
		headerLabel.setText(TaxiGangCreator.taxiGangConstants.step2());

		enterTaxiNumber.setText(TaxiGangCreator.taxiGangConstants.enterATaxiNumber());
		okButton.setText(TaxiGangCreator.taxiGangConstants.cont());
		
		//taxicompanies
		taxiCompanyTable = new FlexTable();
		taxiCompanyTable.setCellSpacing(6);
		FlexCellFormatter cellFormatter = taxiCompanyTable.getFlexCellFormatter();
		//cellFormatter.setColSpan(0, 0, 2);

		ScrollPanel panel = new ScrollPanel(taxiCompanyTable);
		panel.setHeight("120px");
		panel.setWidth("300px");
		panelTaxiCompanies.add(panel);
		
	}

	private void bindUI() {
		bindControlButtons(backButton,PanelType.CREATE_STEP1);
		okButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				String telNo = areaCodeTextBox.getText() + " " +telNoTextBox.getText();
				String meetingMessage = TaxiGangCreator.taxiGangConstants.iHaveCalledThisTaxi() + ":" + telNo;
				Session.getInstance().getSearchTaxiGangInfo().setMeetingMessage(meetingMessage);
				CreateStep3.telNoLabel.setText(telNo);
				taxiGangRoute.setCreateMode(CreateMode.CALL_A_TAXI);
				taxiGangRoute.show(PanelType.CREATE_STEP3);
				
			}
		});
	}

	public void setTaxiCompanyList(TaxiCompanyInfo[] listTaxiCompany) {
		taxiCompanyTable.removeAllRows();
		int row = 0;
		final HashMap<String,TaxiCompanyInfo>mapTaxiCompanyInfo = new HashMap<String,TaxiCompanyInfo>();
		for (TaxiCompanyInfo taxiCompanyInfo : listTaxiCompany){
			String name = taxiCompanyInfo.getName();
			mapTaxiCompanyInfo.put(name,taxiCompanyInfo);
			Button b = new Button(taxiCompanyInfo.getName());
			
			b.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					Object objSrc = event.getSource();
					Button b = (Button)objSrc;
					String name = b.getText();
					final TaxiCompanyInfo taxiCompanyInfo = mapTaxiCompanyInfo.get(name);
					if (taxiCompanyInfo.getUrl() != null){
						Command cmd = new Command(){

							@Override
							public void execute() {
								String meetingMessage = TaxiGangCreator.taxiGangConstants.iHaveOrderedATaxiFrom() + " "+taxiCompanyInfo.getName();
								Session.getInstance().getSearchTaxiGangInfo().setMeetingMessage(meetingMessage);
								taxiGangRoute.show(PanelType.CREATE_STEP4);								
							}
							
						};
						TaxiCompanyDialog.show(taxiCompanyInfo,cmd);
					}else{
						areaCodeTextBox.setText(taxiCompanyInfo.getTelAreaCode());
						telNoTextBox.setText(taxiCompanyInfo.getTelNo());
					}
				}
			});
			String style = bTest.getStyleName();
			b.setStyleName(style);

			//b.setStylePrimaryName("taxiCompanyButton");
			taxiCompanyTable.setWidget(row, 0, b);
			row++;
		}

	}
	
	@Override
	public void show(){
		super.show();
		TaxiGangInfo taxiGangInfo = Session.getInstance().getSearchTaxiGangInfo();
		TaxiGangHandler.getInstance().getTaxiCompany(taxiGangInfo);
		
	}

	
}
